package si.unimb.engineOldGame;

import java.applet.*; 
import java.awt.*; 
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

@SuppressWarnings("serial")
public class LineMaster extends Applet implements Runnable, KeyListener 
{
	
	Thread nit;
	private Image dbImage;
	private Graphics dbg;
	private Lik ballPlayer1;
	private Lik ballPlayer2;
	private boolean gameover;
	private boolean zPlayer1;
	private boolean zPlayer2;
	
	
	// nalaganje vseh elementov igre
	public void init() 
	{
		this.setSize(500, 500);
		addKeyListener(this);
		ballPlayer1 = new Lik (490, 250, 8, 8, true, this.getWidth(), this.getHeight(), Color.GREEN);
		ballPlayer2 = new Lik (10, 250, 8, 8, false, this.getWidth(), this.getHeight(), Color.BLUE);
		gameover = false;
		zPlayer1 = false;
		zPlayer2 = false;
		dbImage = null;
		dbg = null;
	} 
	
	// Metoda ki se zazene ob zagonu appleta
	public void start() 
	{		
		nit = new Thread(this);
		nit.start();
	} 
	
	// Metoda ki se zazene ob izhodu iz appleta
	public void stop() 
	{
		// Ni potrebno
	} 
	
	// Podobno kot zgoraj
	public void destroy() 
	{
		// Ni potrebno
	} 
	
	// Glavna nit
	public void run () 
	{
		
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 
 
		while (true) 
		{ 
			if(!gameover){
				if(!ballPlayer1.move()){
					gameover = true;
					zPlayer2 = true;
				}
				if(!ballPlayer2.move()){
					gameover = true;
					zPlayer1 = true;
				}
				if(ballPlayer1.isCollidingAt(ballPlayer2.getX(), ballPlayer2.getY())){
					ballPlayer2.setDead(true);
					gameover = true;
					zPlayer1 = true;
				}
				if(ballPlayer2.isCollidingAt(ballPlayer1.getX(), ballPlayer1.getY())){
					ballPlayer1.setDead(true);
					gameover = true;
					zPlayer2 = true;
				}
			}
			
			repaint();
			
			try 
			{ 
				Thread.sleep (1000); 
			} 
			catch (InterruptedException ex) { }
			
	 
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 
		}
	}
	
	// DoubleBuffer
	public void update (Graphics g) 
	{
		
		// inicializacija bufferja
		if (dbImage == null) 
		{
			dbImage = createImage (this.getSize().width, this.getSize().height); 
			dbg = dbImage.getGraphics (); 
		} 

		// pocistimo ozadje bufferja
		dbg.setColor (getBackground ()); 
		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); 

		// zapecemo elemente na buffer
		dbg.setColor (getForeground()); 
		paint (dbg); 

		// izrisemo buffer 
		g.drawImage (dbImage, 0, 0, this);
		
	}
	
	// Metoda za izris na applet
	public void paint (Graphics g) 
	{
		if(!gameover){
			ballPlayer1.DrawBall(g);
			ballPlayer2.DrawBall(g);
		}else{
			ballPlayer1.DrawBall(g);
			ballPlayer2.DrawBall(g);
			g.setColor(Color.BLACK);
			g.drawString("Game Over, zmagal je: " + dolociZmagovalca(), 20, 20);
			g.drawString("Pritisni ENTER za ponovno igro!", 165, 400);
		}
		
	}
	
	// Metoda ki doloci zmagovalca
	private String dolociZmagovalca(){
		if(zPlayer1 && !zPlayer2)
			return "Zeleni";
		if(zPlayer2 && !zPlayer1)
			return "Modri";
		if(zPlayer1 && zPlayer2)
			return "Neodloceno";
		else
			return "Napaka!";
	}

	// Metoda za upravlanje z kontrolami
	public void keyPressed(KeyEvent e) 
	{
		
		ballPlayer1.key(e);
		ballPlayer2.key(e);
		
		if(e.getKeyCode() == 10){ // Key :: ENTER
			init();
		}
		
	}

	// Metoda stanja kontole
	public void keyReleased(KeyEvent e) 
	{
		
	}

	// Metoda stanja kontrole
	public void keyTyped(KeyEvent e) 
	{
		
	}

}